<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML/CSS透明背景导出工具</title>
    <script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --primary: #4361ee;
            --secondary: #3f37c9;
            --success: #4cc9f0;
            --dark: #212529;
            --light: #f8f9fa;
            --gray: #6c757d;
            --border: #dee2e6;
            --card-bg: rgba(255, 255, 255, 0.95);
            --preview-bg: #f8f9fa;
            --grid-color: #e9ecef;
        }

        .dark-theme {
            --primary: #4895ef;
            --secondary: #3a0ca3;
            --success: #4cc9f0;
            --dark: #e9ecef;
            --light: #212529;
            --gray: #adb5bd;
            --border: #495057;
            --card-bg: rgba(33, 37, 41, 0.95);
            --preview-bg: #343a40;
            --grid-color: #495057;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #1a2a6c, #b21f1f, #1a2a6c);
            color: var(--dark);
            min-height: 100vh;
            padding: 20px;
            transition: background 0.3s;
        }
        
        .container {
            max-width: 1800px;
            margin: 0 auto;
        }
        
        header {
            text-align: center;
            padding: 30px 0;
            color: white;
            text-shadow: 0 2px 4px rgba(0,0,0,0.3);
            position: relative;
        }
        
        .theme-toggle {
            position: absolute;
            top: 20px;
            right: 20px;
            background: rgba(255,255,255,0.2);
            border: none;
            border-radius: 50px;
            padding: 10px 20px;
            color: white;
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 8px;
            font-weight: 600;
            backdrop-filter: blur(5px);
            transition: all 0.3s;
        }
        
        .theme-toggle:hover {
            background: rgba(255,255,255,0.3);
            transform: translateY(-2px);
        }
        
        header h1 {
            font-size: 2.8rem;
            margin-bottom: 10px;
            background: linear-gradient(90deg, #ff9a9e, #fad0c4, #fad0c4, #a1c4fd);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        header p {
            font-size: 1.2rem;
            max-width: 700px;
            margin: 0 auto;
            opacity: 0.9;
            color: #e9ecef;
        }
        
        .app-container {
            display: flex;
            flex-wrap: wrap;
            gap: 25px;
            margin-top: 20px;
        }
        
        .editor-panel, .preview-panel {
            background: var(--card-bg);
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
            overflow: hidden;
            display: flex;
            flex-direction: column;
            transition: all 0.3s;
        }
        
        .editor-panel {
            flex: 1;
            min-width: 600px;
        }
        
        .preview-panel {
            flex: 1;
            min-width: 600px;
        }
        
        .panel-header {
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            color: white;
            padding: 15px 25px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }
        
        .panel-title {
            font-size: 1.4rem;
            display: flex;
            align-items: center;
            gap: 10px;
            font-weight: 600;
        }
        
        .panel-actions {
            display: flex;
            gap: 15px;
        }
        
        .btn {
            padding: 12px 25px;
            border: none;
            border-radius: 50px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 10px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.2);
            font-size: 1rem;
        }
        
        .btn-primary {
            background: linear-gradient(135deg, #4cc9f0, #4361ee);
            color: white;
        }
        
        .btn-primary:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 20px rgba(0,0,0,0.3);
        }
        
        .btn-success {
            background: linear-gradient(135deg, #2ecc71, #27ae60);
            color: white;
        }
        
        .btn-success:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 20px rgba(0,0,0,0.3);
        }
        
        .btn-outline {
            background: transparent;
            border: 2px solid white;
            color: white;
        }
        
        .btn-outline:hover {
            background: rgba(255,255,255,0.2);
        }
        
        .code-container {
            display: flex;
            flex-direction: column;
            gap: 25px;
            padding: 25px;
            flex: 1;
        }
        
        .code-section {
            display: flex;
            flex-direction: column;
            flex: 1;
            position: relative;
        }
        
        .code-label {
            font-weight: 600;
            margin-bottom: 15px;
            color: var(--primary);
            display: flex;
            align-items: center;
            gap: 10px;
            font-size: 1.1rem;
            padding-left: 5px;
        }
        
        textarea {
            flex: 1;
            padding: 20px;
            border: 2px solid var(--border);
            border-radius: 12px;
            font-family: 'Fira Code', 'Consolas', monospace;
            font-size: 1rem;
            resize: none;
            transition: all 0.3s;
            background: var(--light);
            color: var(--dark);
            line-height: 1.6;
        }
        
        textarea:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 4px rgba(67, 97, 238, 0.2);
        }
        
        .preview-content {
            flex: 1;
            padding: 20px;
            overflow: auto;
            background-color: var(--preview-bg);
            position: relative;
            background-image: 
                linear-gradient(45deg, var(--grid-color) 25%, transparent 25%),
                linear-gradient(-45deg, var(--grid-color) 25%, transparent 25%),
                linear-gradient(45deg, transparent 75%, var(--grid-color) 75%),
                linear-gradient(-45deg, transparent 75%, var(--grid-color) 75%);
            background-size: 20px 20px;
            background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
        }
        
        .preview-frame {
            width: 100%;
            height: 100%;
            border: none;
            background-color: transparent;
            min-height: 500px;
        }
        
        .export-overlay {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.85);
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            color: white;
            z-index: 100;
            opacity: 0;
            pointer-events: none;
            transition: opacity 0.3s;
            text-align: center;
            padding: 20px;
        }
        
        .export-overlay.active {
            opacity: 1;
            pointer-events: all;
        }
        
        .spinner {
            width: 60px;
            height: 60px;
            border: 5px solid rgba(255,255,255,0.3);
            border-radius: 50%;
            border-top-color: white;
            animation: spin 1s linear infinite;
            margin-bottom: 25px;
        }
        
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
        
        footer {
            text-align: center;
            padding: 30px 0;
            color: rgba(255,255,255,0.7);
            font-size: 0.9rem;
            margin-top: 40px;
        }
        
        .features {
            display: flex;
            justify-content: center;
            gap: 40px;
            flex-wrap: wrap;
            margin: 40px 0;
        }
        
        .feature {
            background: rgba(255,255,255,0.15);
            backdrop-filter: blur(10px);
            border-radius: 15px;
            padding: 25px;
            width: 280px;
            color: white;
            text-align: center;
            transition: all 0.3s;
            border: 1px solid rgba(255,255,255,0.1);
        }
        
        .feature:hover {
            transform: translateY(-8px);
            box-shadow: 0 15px 30px rgba(0,0,0,0.2);
        }
        
        .feature i {
            font-size: 3rem;
            margin-bottom: 20px;
            background: linear-gradient(135deg, #4cc9f0, #4361ee);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        .feature h3 {
            margin-bottom: 15px;
            font-size: 1.4rem;
        }
        
        .feature p {
            font-size: 1rem;
            opacity: 0.9;
        }
        
        .controls-container {
            padding: 25px;
            display: flex;
            flex-direction: column;
            gap: 25px;
        }
        
        .control-group {
            background: rgba(67, 97, 238, 0.05);
            padding: 25px;
            border-radius: 15px;
            border: 1px solid var(--border);
        }
        
        .control-group h3 {
            margin-bottom: 20px;
            color: var(--primary);
            display: flex;
            align-items: center;
            gap: 12px;
            font-size: 1.3rem;
        }
        
        .settings {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }
        
        .setting-item {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 12px 0;
            border-bottom: 1px dashed var(--border);
        }
        
        .setting-item label {
            color: var(--dark);
            font-size: 1rem;
            font-weight: 500;
        }
        
        .switch {
            position: relative;
            display: inline-block;
            width: 60px;
            height: 28px;
        }
        
        .switch input {
            opacity: 0;
            width: 0;
            height: 0;
        }
        
        .slider {
            position: absolute;
            cursor: pointer;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: #ccc;
            transition: .4s;
            border-radius: 34px;
        }
        
        .slider:before {
            position: absolute;
            content: "";
            height: 20px;
            width: 20px;
            left: 4px;
            bottom: 4px;
            background-color: white;
            transition: .4s;
            border-radius: 50%;
        }
        
        input:checked + .slider {
            background: linear-gradient(135deg, var(--primary), var(--secondary));
        }
        
        input:checked + .slider:before {
            transform: translateX(32px);
        }
        
        .size-controls {
            display: flex;
            gap: 15px;
            margin-top: 15px;
        }
        
        .size-input {
            flex: 1;
            padding: 12px 15px;
            border-radius: 8px;
            border: 2px solid var(--border);
            background: var(--light);
            color: var(--dark);
            font-size: 1rem;
        }
        
        .size-input:focus {
            outline: none;
            border-color: var(--primary);
        }
        
        .info-box {
            background: rgba(67, 97, 238, 0.05);
            padding: 20px;
            border-radius: 15px;
            font-size: 0.95rem;
            line-height: 1.7;
            color: var(--dark);
            border: 1px solid var(--border);
        }
        
        .info-box h3 {
            color: var(--primary);
            margin-bottom: 15px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .info-box ul {
            padding-left: 25px;
            margin-top: 15px;
        }
        
        .info-box li {
            margin-bottom: 12px;
            position: relative;
            padding-left: 15px;
        }
        
        .info-box li:before {
            content: "•";
            color: var(--primary);
            position: absolute;
            left: 0;
            font-size: 1.2rem;
        }
        
        .highlight {
            background: rgba(67, 97, 238, 0.1);
            padding: 2px 6px;
            border-radius: 4px;
            color: var(--primary);
            font-weight: 600;
        }
        
        .logo {
            display: flex;
            justify-content: center;
            margin-bottom: 20px;
        }
        
        .logo-inner {
            width: 80px;
            height: 80px;
            background: linear-gradient(135deg, #4361ee, #3a0ca3);
            border-radius: 20px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 2.5rem;
            box-shadow: 0 10px 20px rgba(0,0,0,0.2);
        }
        
        @media (max-width: 1300px) {
            .app-container {
                flex-direction: column;
            }
            
            .editor-panel, .preview-panel {
                width: 100%;
                min-width: auto;
            }
        }
        
        @media (max-width: 768px) {
            header h1 {
                font-size: 2rem;
                padding: 0 20px;
            }
            
            .panel-title {
                font-size: 1.2rem;
            }
            
            .btn {
                padding: 10px 18px;
                font-size: 0.9rem;
            }
            
            .theme-toggle {
                position: relative;
                top: 0;
                right: 0;
                margin: 20px auto;
                width: max-content;
            }
            
            .features {
                gap: 20px;
            }
            
            .feature {
                width: 100%;
                max-width: 400px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <div class="logo">
                <div class="logo-inner">
                    <i class="fas fa-code"></i>
                </div>
            </div>
            <h1>HTML/CSS 透明背景导出工具</h1>
            <p>实时编辑HTML和CSS代码，预览效果并导出为透明背景的PNG图片</p>
            
            <button class="theme-toggle" id="themeToggle">
                <i class="fas fa-moon"></i> 切换到深色模式
            </button>
            
            <div class="features">
                <div class="feature">
                    <i class="fas fa-sync-alt"></i>
                    <h3>实时预览</h3>
                    <p>即时查看代码效果，所见即所得</p>
                </div>
                <div class="feature">
                    <i class="fas fa-file-image"></i>
                    <h3>透明背景导出</h3>
                    <p>完美支持透明背景导出为PNG</p>
                </div>
                <div class="feature">
                    <i class="fas fa-mobile-alt"></i>
                    <h3>响应式设计</h3>
                    <p>在手机、平板和电脑上均可使用</p>
                </div>
            </div>
        </header>
        
        <div class="app-container">
            <div class="editor-panel">
                <div class="panel-header">
                    <div class="panel-title">
                        <i class="fas fa-code"></i> 代码编辑器
                    </div>
                    <div class="panel-actions">
                        <button id="saveBtn" class="btn btn-primary">
                            <i class="fas fa-sync"></i> 更新预览
                        </button>
                    </div>
                </div>
                <div class="code-container">
                    <div class="code-section">
                        <div class="code-label">
                            <i class="fab fa-html5"></i> HTML 代码
                        </div>
                        <textarea id="htmlCode" placeholder="在此输入HTML代码...">&lt;div class="transparent-card"&gt;
  &lt;div class="card-header"&gt;
    &lt;h2&gt;透明背景导出示例&lt;/h2&gt;
    &lt;p&gt;此卡片具有透明背景效果&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="card-content"&gt;
    &lt;div class="logo"&gt;
      &lt;i class="fas fa-code"&gt;&lt;/i&gt;
    &lt;/div&gt;
    &lt;p&gt;在下方输入HTML和CSS代码，右侧实时预览效果&lt;/p&gt;
    &lt;div class="features"&gt;
      &lt;div class="feature"&gt;&lt;i class="fas fa-check"&gt;&lt;/i&gt; 透明背景支持&lt;/div&gt;
      &lt;div class="feature"&gt;&lt;i class="fas fa-check"&gt;&lt;/i&gt; 高质量导出&lt;/div&gt;
      &lt;div class="feature"&gt;&lt;i class="fas fa-check"&gt;&lt;/i&gt; 实时预览&lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="card-footer"&gt;
    &lt;button class="btn"&gt;&lt;i class="fas fa-download"&gt;&lt;/i&gt; 导出为PNG&lt;/button&gt;
  &lt;/div&gt;
&lt;/div&gt;</textarea>
                    </div>
                    <div class="code-section">
                        <div class="code-label">
                            <i class="fab fa-css3-alt"></i> CSS 代码
                        </div>
                        <textarea id="cssCode" placeholder="在此输入CSS代码...">/* 确保body背景透明 */
body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: transparent !important;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  padding: 20px;
}

.transparent-card {
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  max-width: 500px;
  width: 100%;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.card-header {
  background: linear-gradient(135deg, #4361ee, #3a0ca3);
  color: white;
  padding: 30px;
  text-align: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}

.card-header h2 {
  margin: 0 0 10px 0;
  font-size: 28px;
}

.card-header p {
  opacity: 0.9;
  font-size: 18px;
}

.card-content {
  padding: 30px;
  text-align: center;
}

.logo {
  font-size: 60px;
  color: #4361ee;
  margin-bottom: 25px;
}

.card-content p {
  color: #495057;
  line-height: 1.7;
  margin-bottom: 25px;
  font-size: 18px;
}

.features {
  display: flex;
  flex-direction: column;
  gap: 15px;
  margin-bottom: 25px;
}

.feature {
  background: rgba(67, 97, 238, 0.1);
  color: #4361ee;
  padding: 12px 20px;
  border-radius: 10px;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  font-weight: 500;
}

.card-footer {
  padding: 20px;
  text-align: center;
  border-top: 1px solid rgba(0,0,0,0.05);
}

.btn {
  background: linear-gradient(135deg, #4361ee, #4cc9f0);
  color: white;
  border: none;
  padding: 15px 40px;
  border-radius: 50px;
  cursor: pointer;
  font-size: 18px;
  font-weight: 600;
  transition: all 0.3s;
  display: inline-flex;
  align-items: center;
  gap: 12px;
  box-shadow: 0 5px 15px rgba(67, 97, 238, 0.3);
}

.btn:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 20px rgba(67, 97, 238, 0.4);
}</textarea>
                    </div>
                </div>
            </div>
            
            <div class="preview-panel">
                <div class="panel-header">
                    <div class="panel-title">
                        <i class="fas fa-eye"></i> 预览效果
                    </div>
                    <div class="panel-actions">
                        <button id="exportBtn" class="btn btn-success">
                            <i class="fas fa-download"></i> 导出透明PNG
                        </button>
                    </div>
                </div>
                <div class="preview-content">
                    <iframe id="previewFrame" class="preview-frame"></iframe>
                    <div id="exportOverlay" class="export-overlay">
                        <div class="spinner"></div>
                        <h2>正在生成透明背景图片</h2>
                        <p>请稍候，这可能需要几秒钟时间...</p>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="controls-container">
            <div class="control-group">
                <h3><i class="fas fa-lightbulb"></i> 透明背景导出指南</h3>
                <div class="info-box">
                    <p>要正确导出透明背景图片，请确保在CSS中设置：</p>
                    <ul>
                        <li>在<strong>body元素</strong>上设置<span class="highlight">background: transparent</span></li>
                        <li>避免使用不透明的背景颜色</li>
                        <li>使用<span class="highlight">rgba()</span>颜色值代替十六进制颜色</li>
                        <li>网格背景仅用于预览，不会包含在导出图片中</li>
                        <li>导出的PNG图片将保留所有透明区域</li>
                    </ul>
                </div>
            </div>
        </div>
        
        <footer>
            <p>© 2023 HTML/CSS透明背景导出工具 | 已修复导出问题</p>
        </footer>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const htmlCode = document.getElementById('htmlCode');
            const cssCode = document.getElementById('cssCode');
            const previewFrame = document.getElementById('previewFrame');
            const saveBtn = document.getElementById('saveBtn');
            const exportBtn = document.getElementById('exportBtn');
            const exportOverlay = document.getElementById('exportOverlay');
            const themeToggle = document.getElementById('themeToggle');
            
            // 初始化预览
            updatePreview();
            
            // 主题切换
            themeToggle.addEventListener('click', function() {
                document.body.classList.toggle('dark-theme');
                const isDark = document.body.classList.contains('dark-theme');
                themeToggle.innerHTML = isDark ? 
                    '<i class="fas fa-sun"></i> 切换到浅色模式' : 
                    '<i class="fas fa-moon"></i> 切换到深色模式';
            });
            
            // 保存并预览按钮事件
            saveBtn.addEventListener('click', updatePreview);
            
            // 导出为图片按钮事件
            exportBtn.addEventListener('click', exportAsImage);
            
            // 更新预览
            function updatePreview() {
                const html = htmlCode.value;
                const css = cssCode.value;
                
                const fullHtml = `
                    <!DOCTYPE html>
                    <html>
                    <head>
                        <meta charset="UTF-8">
                        <meta name="viewport" content="width=device-width, initial-scale=1.0">
                        <style>
                            /* 强制设置透明背景 */
                            html, body {
                                background: transparent !important;
                                margin: 0;
                                padding: 0;
                                min-height: 100vh;
                                width: 100%;
                            }
                            
                            /* 用户样式 */
                            ${css}
                        </style>
                    </head>
                    <body>
                        ${html}
                    </body>
                    </html>
                `;
                
                // 将完整HTML写入iframe
                previewFrame.srcdoc = fullHtml;
            }
            
            // 导出为透明背景图片
            function exportAsImage() {
                // 显示加载指示器
                exportOverlay.classList.add('active');
                
                // 等待iframe内容加载完成
                previewFrame.addEventListener('load', function onLoad() {
                    previewFrame.removeEventListener('load', onLoad);
                    
                    // 使用html2canvas将iframe内容转换为图片
                    html2canvas(previewFrame.contentDocument.documentElement, {
                        scale: 2,
                        backgroundColor: null, // 关键：设置为透明背景
                        useCORS: true,
                        allowTaint: true,
                        logging: false,
                        onclone: (clonedDoc) => {
                            // 确保克隆文档的背景透明
                            clonedDoc.documentElement.style.background = 'transparent';
                            clonedDoc.body.style.background = 'transparent';
                        }
                    }).then(canvas => {
                        // 创建下载链接
                        const link = document.createElement('a');
                        link.download = 'transparent-export.png';
                        link.href = canvas.toDataURL('image/png');
                        document.body.appendChild(link);
                        link.click();
                        document.body.removeChild(link);
                        
                        // 隐藏加载指示器
                        exportOverlay.classList.remove('active');
                    }).catch(error => {
                        console.error('导出图片失败:', error);
                        alert('导出失败: ' + error.message);
                        exportOverlay.classList.remove('active');
                    });
                });
                
                // 确保iframe内容是最新的
                updatePreview();
            }
        });
    </script>
</body>
</html>